{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8cae0a4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import graphviz\n",
    "from itertools import product\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "35860d69",
   "metadata": {},
   "outputs": [],
   "source": [
    "table2_array = pd.Series(np.load('../Database/table2_array.npy'))\n",
    "table3_array = pd.Series(np.load('../Database/table3_array.npy'))\n",
    "\n",
    "def policy_3(input1, input2, input3):\n",
    "    a = round(input1)\n",
    "    b = round(input2)\n",
    "    c = round(input3)\n",
    "    return table3_array[25*(a-1)+5*(b-1)+(c-1)].values\n",
    "def policy_2(input1, input2):\n",
    "    a = round(input1)\n",
    "    b = round(input2)\n",
    "    return table2_array[5*(a-1) + (b-1)].values\n",
    "\n",
    "colors = ['red','orange','green','blue','black']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2aa5a58c",
   "metadata": {},
   "source": [
    "Plot the corresponding DAG of the 3-way decision table used in the original HES"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5be610cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "table = [[str(i),str(j),str(k)] for i,j,k in product(range(5), range(5), range(5))]\n",
    "table2 = [[str(25*i+5*j+k)] for i,j,k in product(range(5), range(5), range(5))]\n",
    "colortable = [colors[table3_array[25*i+5*j+k]-1] for i,j,k in product(range(5), range(5), range(5))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1c6540a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "DG = graphviz.Digraph(comment='Partial Order')\n",
    "for i in range(125):\n",
    "    x=table[i]\n",
    "    xcolor = colortable[i]\n",
    "    DG.node(''.join(x), color=xcolor)\n",
    "for i,j,k in product( range(5),range(5),range(4)):\n",
    "    DG.edge(''.join(table[25*i+5*j+k]),''.join(table[25*i+5*j+(k+1)]))\n",
    "for i,j,k in product( range(5),range(4),range(5)):\n",
    "    DG.edge(''.join(table[25*i+5*j+k]),''.join(table[25*i+5*(j+1)+k]))\n",
    "for i,j,k in product( range(4),range(5),range(5)):\n",
    "    DG.edge(''.join(table[25*i+5*j+k]),''.join(table[25*(i+1)+5*j+k]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "620ae912",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Digraph.gv.pdf'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DG.render('table3')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55251280",
   "metadata": {},
   "source": [
    "Plot the corresponding DAG of the 2-way decision table used in the original HES"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f0c53c4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "table = [[str(i+1),str(j+1)] for i,j in product(range(5), range(5))]\n",
    "table2 = [[str(5*i+j)] for i,j in product(range(5), range(5))]\n",
    "colortable = [colors[table2_array[5*i+j]-1] for i,j in product(range(5), range(5))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3ee9335c",
   "metadata": {},
   "outputs": [],
   "source": [
    "DG = graphviz.Digraph(comment='Partial Order',graph_attr={'rankdir':'LR'})\n",
    "for i in range(25):\n",
    "    x=table[i]\n",
    "    xcolor = colortable[i]\n",
    "    DG.node('['+','.join(x)+']', color=xcolor)\n",
    "for i,j in product( range(5),range(4)):\n",
    "    DG.edge('['+','.join(table[5*i+j])+']','['+','.join(table[5*i+j+1])+']')\n",
    "for i,j in product( range(4),range(5)):\n",
    "    DG.edge('['+','.join(table[5*i+j])+']','['+','.join(table[5*(i+1)+j])+']')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "90c78907",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'table2.pdf'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DG.render('table2')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d381c757",
   "metadata": {},
   "source": [
    "Plotting another example of the 2-way decision table that doesn't satisfy the monotonicity condition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "67219397",
   "metadata": {},
   "outputs": [],
   "source": [
    "DG = graphviz.Digraph(comment='Partial Order',graph_attr={'rankdir':'LR'})\n",
    "for i in range(25):\n",
    "    x=table[i]\n",
    "    xcolor = colortable[i]\n",
    "    if i in [10,15]:\n",
    "        xcolor='green'\n",
    "    DG.node('['+','.join(x)+']', color=xcolor)\n",
    "for i,j in product( range(5),range(4)):\n",
    "    DG.edge('['+','.join(table[5*i+j])+']','['+','.join(table[5*i+j+1])+']')\n",
    "for i,j in product( range(4),range(5)):\n",
    "    DG.edge('['+','.join(table[5*i+j])+']','['+','.join(table[5*(i+1)+j])+']')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8b09a797",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'table22.pdf'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DG.render('table2_not_monotonic')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bf8e786",
   "metadata": {},
   "source": [
    "Plotting legend for the previos figure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "922f6cc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "legend = graphviz.Digraph(comment='Partial Order',graph_attr={'rankdir':'LR'})\n",
    "  # Add the legend items as nodes with different shapes and colors\n",
    "for i in range(5):\n",
    "    legend.node('legend_label_'+str(i+1), 'Output is '+str(i+1),width='0.01',height='0.01', shape='none')\n",
    "    legend.node('legend_item_'+str(i+1), '',  color=['red','orange','green','blue','black'][i])   \n",
    "    legend.edge('legend_item_'+str(i+1), 'legend_label_'+str(i+1), style='invis')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b69a7716",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'legend2.pdf'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "legend.render('legend2')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
